TITLE PDP-8 Instruction Parser						(main.asm)

; By Richard Armijo

INCLUDE Irvine32.inc
.data

memory WORD 0000000000000001b, 0000001110000010b, 0000010000000100b, 0000011100001000b, 0000100010010000b, 0000101100100000b, 0000110001000000b, 0000111111111111b, 627h, 367h, 198h, 9F2h, 0h, 4095
swapSpace DWORD 0h
octalStore WORD 0h
msgAnd BYTE "AND", 0
msgTad BYTE "TAD", 0
msgIsz BYTE "ISZ", 0
msgDca BYTE "DCA", 0
msgJms BYTE "JMS", 0
msgJmp BYTE "JMP", 0
msgIot BYTE "IOT", 0
msgOpr BYTE "OPR", 0
msgJustASpace BYTE " ", 0

.code

setFirstLocation PROC	;Points to where PDP8 memory begins
	mov edx, OFFSET memory
	ret
setFirstLocation ENDP

incLocation PROC		;Increments the value being pointed at in edx
	add edx, 2
	ret
incLocation ENDP

getMemoryValue PROC	;Returns the value at location edx in ax
	mov ax, [edx]
	ret
getMemoryValue ENDP

isolateInstruction PROC	;Takes a memory value in ax; returns the 3-bit instruction in ax
	shr ax, 9
	ret
isolateInstruction ENDP

isolateAddress PROC		;Takes a memory value in ax; returns the 7-bit address in ax
	and ax, 0000000001111111b
	ret
isolateAddress ENDP

isolateIndirectBit PROC	;Takes a memory value in ax; returns the indirect bit in ax
	and ax, 0000000100000000b
	shr ax, 8
	ret
isolateIndirectBit ENDP

isolatePageBit PROC	;Takes a memory value in ax; returns the page bit in ax
	and ax, 0000000010000000b
	shr ax, 7
	ret
isolatePageBit ENDP

writeOctal PROC		;Writes a 12-bit value in octal
	mov octalStore, ax
	and ax, 0000111000000000b
	shr ax, 9
	call WriteDec
	mov ax, octalStore
	and ax, 0000000111000000b
	shr ax, 6
	call WriteDec
	mov ax, octalStore
	and ax, 0000000000111000b
	shr ax, 3
	call WriteDec
	mov ax, octalStore
	and ax, 0000000000000111b
	call WriteDec
	ret
writeOctal ENDP

checkInstruction PROC	;Takes an instruction in ax; tells you the opcode

	mov swapSpace, edx

	cmp ax, 0000000000000000b
	jne notAnd
	mov edx, OFFSET msgAnd
notAnd:
	
	cmp ax, 0000000000000001b
	jne notTad
	mov edx, OFFSET msgTad
notTad:

	cmp ax, 0000000000000010b
	jne notIsz
	mov edx, OFFSET msgIsz
notIsz:
	
	cmp ax, 0000000000000011b
	jne notDca
	mov edx, OFFSET msgDca
notDca:

	cmp ax, 0000000000000100b
	jne notJms
	mov edx, OFFSET msgJms
notJms:
	
	cmp ax, 0000000000000101b
	jne notJmp
	mov edx, OFFSET msgJmp
notJmp:

	cmp ax, 0000000000000110b
	jne notIot
	mov edx, OFFSET msgIot
notIot:
	
	cmp ax, 0000000000000111b
	jne notOpr
	mov edx, OFFSET msgOpr
notOpr:

	call WriteString
	
	mov edx, swapSpace
	
	ret
checkInstruction ENDP

main PROC
	mov ecx, LENGTHOF memory	
	call setFirstLocation
	tempLoop:
		call getMemoryValue
		call isolateInstruction
		call checkInstruction
		call Crlf
		call getMemoryValue
		call isolateIndirectBit
		call WriteDec
		call Crlf
		call getMemoryValue
		call isolatePageBit
		call WriteDec
		call Crlf
		call getMemoryValue
		call isolateAddress
		call writeOctal
		call Crlf
		call Crlf
		call incLocation
	loop tempLoop
	exit
main ENDP

END main